/**
 * Copyright © 2014 TingerSure. All rights reserved.
 */
package org.sure.scomet.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.sure.scomet.action.CloseAction;
import org.sure.scomet.action.ConnectionAction;
import org.sure.scomet.action.MessageAction;
import org.sure.scomet.action.RequestAction;
import org.sure.scomet.action.UnknownRequestException;
import org.sure.scomet.link.Connection;
import org.sure.scomet.link.ConnectionCenter;
import org.sure.scomet.util.ConfigManager;
import org.sure.scomet.util.PageIdManager;
import org.sure.scomet.util.log.LogManager;
import org.sure.scomet.util.log.LogType;

public class ServerCenter implements Filter {

	/**
	 * The URL end with urlTail will be filtered to use.
	 */
	private static String urlTail = null;

	/**
	 * Destroy all server classes.
	 */
	@Override
	public void destroy() {
		ConnectionCenter.destroy();
		Connection.destroy();
		LogManager.log("SComet destroy.", LogType.info);
		PageIdManager.destroy();
		LogManager.destroy();
		ConfigManager.destroy();
		urlTail = null;
		character = null;
	}

	/**
	 * the project character.
	 */
	public static String character = null;

	/**
	 * Filter all requests, find out the request which the framework needed to
	 * use, release the other requests.
	 */

	@Override
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		HttpServletResponse res = (HttpServletResponse) response;
		HttpServletRequest req = (HttpServletRequest) request;
		String url = req.getRequestURL().toString();
		if (url.endsWith("." + urlTail)) {
			LogManager.log(url, LogType.debug);
			req.setCharacterEncoding(character);
			res.setCharacterEncoding(character);
			String actionName = url.substring(url.lastIndexOf('/') + 1,
					url.lastIndexOf('.'));
			if ("connection".equals(actionName)) {
				new ConnectionAction().Action(req, res);
			} else if ("message".equals(actionName)) {
				new MessageAction().Action(req, res);
			} else if ("close".equals(actionName)) {
				new CloseAction().Action(req, res);
			} else if ("request".equals(actionName)) {
				new RequestAction().Action(req, res);
			} else {
				LogManager.logException("unknown request",
						new UnknownRequestException(url), LogType.error);
			}
			return;
		}
		chain.doFilter(request, response);
	}

	/**
	 * Initialize all server classes, get attributes from the configuration.
	 */
	@Override
	public void init(FilterConfig config) throws ServletException {
		ConfigManager.init();
		LogManager.init();
		PageIdManager.init();
		ConnectionCenter.init();
		Connection.init();
		urlTail = ConfigManager.getConfig("url-tail", "scomet");
		character = ConfigManager.getConfig("character", "utf-8");
		LogManager.log("SComet successfully initialized.", LogType.info);
	}

}
